package com.wc.AlgoOJ_train.train1.c8.A803;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/1/11 17:34
 * @description 选课时间
 * http://43.138.190.70:8888/p/A803
 */
public class Main {
    static FastReader sc = new FastReader();

    public static void main(String[] args) {
        int t = sc.nextInt();
        while (t-- > 0) {
            int n = sc.nextInt();
            int k = sc.nextInt();
            int[][] courses = new int[k][2];
            for (int i = 0; i < k; i++) {
                courses[i][0] = sc.nextInt();
                courses[i][1] = sc.nextInt();
            }
            int[][] dp = new int[k][n + 1];
            for (int i = 0; i <= courses[0][1] && i * courses[0][0] <= n; i++) {
                dp[0][i * courses[0][0]] = 1;
            }
            for (int i = 1; i < k; i++) {
                for (int j = 0; j <= n; j++) {
                    int num = 0;
                    // 多重背包限制个数
                    while (j >= num * courses[i][0] && num <= courses[i][1]) {
                        dp[i][j] += dp[i - 1][j - num * courses[i][0]];
                        num++;
                    }
                }
            }
            System.out.println(dp[k - 1][n]);
        }
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }
}
